<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题目标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入题目标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="章节" prop="subjectChapterDeptId">
        <treeselect v-model="queryParams.subjectChapterDeptId" :options="subjectChapterDeptIddeptOptions"
          :show-count="true" placeholder="请选择章节"  class="w200" @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="题目类型" prop="type">
        <!-- <el-input v-model="queryParams.type" placeholder="请输入1单选  2多选  3判断" clearable class="w200"
          @keyup.enter.native="handleQuery" /> -->

        <el-select v-model="queryParams.type" placeholder="请选择题目类型" class="w200" @keyup.enter.native="handleQuery">
          <el-option v-for="item in type" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="难易程度" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择难易程度" class="w200" @keyup.enter.native="handleQuery">
          <el-option v-for="item in level" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
        <!-- <el-input v-model="queryParams.level" placeholder="请输入1简单 2中等 3困难" clearable @keyup.enter.native="handleQuery"
          class="w200" /> -->
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:bank:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:bank:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:bank:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:bank:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bankList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="id" width="80"  />
      <!-- <el-table-column prop="subjectDeptId" label="专业" width="80" align="center">
        <template slot-scope="scope">
          {{ bindCourse(scope.row.subjectDeptId) }}
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="课程" align="center" prop="subjectId" /> -->
      <el-table-column prop="subjectChapterDeptId" label="章节" width="120" align="center">
        <template slot-scope="scope">
          <!-- {{ bindChapter(scope.row.subjectChapterDeptId) }} -->
          {{ scope.row.chapterName||'-'  }}
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="题目类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">单选</el-tag>
          <el-tag type="success" v-if="scope.row.type == 2">多选</el-tag>
          <el-tag type="primary" v-if="scope.row.type == 3">判断</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="难易程度" align="center" prop="level" width="120">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.level" disabled show-score text-color="#ff9900" score-template="" :max="3">
          </el-rate>
        </template>

      </el-table-column>
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="解析" align="center" prop="remark" width="300" />

      <el-table-column label="操作" align="center" fixed="right" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bank:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:bank:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改题库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="题目" prop="title">
          <el-input v-model="form.title" placeholder="请输入题目标题" />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <!-- <el-form-item label="章节名称" prop="deptName">
                <el-input v-model="form.deptName" placeholder="请输入章节名称" />
              </el-form-item> -->
            <el-form-item label="章节" prop="subjectChapterDeptId">
              <treeselect v-model="form.subjectChapterDeptId" :options="subjectChapterDeptIddeptOptions"
                :show-count="true" placeholder="请选择章节" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="题型" prop="type">
              <el-select v-model="form.type" placeholder="清选择题型" clearable @change="typeChange" :disabled="formTypeState">
                <el-option v-for="dict in dict.type.sys_answer_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="难度" prop="level">
              <el-select v-model="form.level" placeholder="清选择" clearable>
                <el-option v-for="dict in dict.type.sys_answer_level" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="120" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="A" prop="optionA">
              <el-input v-model="form.optionA" placeholder="请输入选项A内容" />

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="B" prop="optionB">
              <el-input v-model="form.optionB" placeholder="请输入选项B内容" />

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="C" prop="optionC" v-if="optionCount > 2">
              <el-input v-model="form.optionC" placeholder="请输入选项C内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="D" prop="optionD" v-if="optionCount > 3">
              <el-input v-model="form.optionD" placeholder="请输入选项D内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="E" prop="optionE" v-if="optionCount > 4">
              <el-input v-model="form.optionE" placeholder="请输入选项E内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="F" prop="optionF" v-if="optionCount > 5">
              <el-input v-model="form.optionF" placeholder="请输入选项F内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="新增选项">
          <el-button type="primary" icon="el-icon-plus" style="width: 680px;" @click="addOption"
            :disabled="optionCount >= 6"> </el-button>
        </el-form-item>



        <el-form-item label="正确答案" v-show="form.type == 2">
          <el-select v-model="correctAnswerMul" multiple placeholder="可选择多个答案" style="width: 680px;"
            @change="correctAnswerSelectChange">
            <el-option v-for="item in correctAnswerList" :key="item.key" :label="item.label" :value="item.key">
            </el-option>
          </el-select>

        </el-form-item>

        <el-form-item label="正确答案" v-show="form.type == 1 || form.type == 3">
          <el-select v-model="correctAnswer" placeholder="请选择一个答案" style="width: 680px;"
            @change="correctAnswerSelectChange">
            <el-option v-for="item in correctAnswerList" :key="item.key" :label="item.label" :value="item.key">
            </el-option>
          </el-select>

        </el-form-item>


        <el-form-item label="解析">
          <editor v-model="form.remark" :min-height="192" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBank, getBank, delBank, addBank, updateBank } from "@/api/course/bank";
import { chapterDeptTreeSelect } from "@/api/course/chapter";
import Treeselect from "@riophae/vue-treeselect";

import { deptTreeSelect } from "@/api/course/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Bank",
  components: { Treeselect },
  dicts: ['sys_normal_disable', 'sys_answer_type', 'sys_answer_level', 'sys_answer_option_yn'],
  props: {
    subId: {
      type: Number
    },
    deptId: {
      type: Number
    }
  },
  data() {
    return {
      test: 3,
      subjectId: this.subId,
      subjectDeptId: this.deptId,
      formTypeState: false,
      correctAnswer: undefined,
      correctAnswerMul: undefined,
      type: [{
        value: '1',
        label: '单选'
      }, {
        value: '2',
        label: '多选'
      }, {
        value: '3',
        label: '判断'
      }],
      level: [{
        value: '1',
        label: '简单'
      }, {
        value: '2',
        label: '一般'
      }, {
        value: '3',
        label: '困难'
      }],
      correctAnswerList: [
        { key: 1, label: `选项A`, correct: false },
        { key: 2, label: `选项B`, correct: false },
        { key: 3, label: `选项C`, correct: false },
        { key: 4, label: `选项D`, correct: false },
        { key: 5, label: `选项E`, correct: false },
        { key: 6, label: `选项F`, correct: false },
      ],
      optionCount: 4,

      subjectChapterDeptIddeptOptions: [],
      CoursedeptOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题库表格数据
      bankList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        subTitle: null,
        content: null,
        type: null,
        optionA: null,
        optionB: null,
        optionC: null,
        optionD: null,
        optionE: null,
        optionF: null,
        correctA: null,
        correctB: null,
        correctC: null,
        correctD: null,
        correctE: null,
        correctF: null,
        status: null,
        level: null,
        class: null,
        subjectId: this.subId,
        subjectDeptId: null,
        subjectChapterDeptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.CoursedeptTreeSelect();
  },
  methods: {
    bindCourse(id) {
      let courseName = '-';
      if (this.subjectChapterDeptIddeptOptions != null && this.subjectChapterDeptIddeptOptions[0].children != null) {
        this.subjectChapterDeptIddeptOptions[0].children.forEach(item => {
          if (id == item.id) {
            courseName = item.label
          }
          if (item.children != null) {
            item.children.forEach(item1 => {
              if (id == item1.id) {
                courseName = item.label + '-' + item1.label
              }
              if (item1.children != null) {
                item1.children.forEach(item2 => {
                  courseName = item.label + '-' + item1.label + '-' + item2.label
                })
              }
            })
          }
        })
      }

      return courseName;
    },
    bindChapter(id) {
      let chapterName = '-';
      if (this.subjectChapterDeptIddeptOptions != null && this.subjectChapterDeptIddeptOptions[0].children != null) {
        this.subjectChapterDeptIddeptOptions[0].children.forEach(item => {
          if (id == item.id) {
            chapterName = item.label
          }
          if (item.children != null) {
            item.children.forEach(item1 => {
              if (id == item1.id) {
                chapterName = item.label + '-' + item1.label
              }
              if (item1.children != null) {
                item1.children.forEach(item2 => {
                  chapterName = item.label + '-' + item1.label + '-' + item2.label
                })
              }
            })
          }
        })
      }

      return chapterName;
    },
    CoursedeptTreeSelect() {
      deptTreeSelect().then(response => {
        this.CoursedeptOptions = response.data;
      });
    },
    /** 查询章节下拉树结构 */
    getDeptTree() {
      chapterDeptTreeSelect( { 
        subjectId: this.subjectId 
      }).then(response => {
        this.subjectChapterDeptIddeptOptions = response.data[0].children;
      });
    },
    correctAnswerSelectChange(e) {
      this.formTypeState = true;
      console.log(e)
      this.form.correctA = 0;
      this.form.correctB = 0;
      this.form.correctC = 0;
      this.form.correctD = 0;
      this.form.correctE = 0;
      this.form.correctF = 0;
      if (Array.isArray(e)) {
        e.forEach(el => {
          this.setAnswer(el)
        });
      } else {
        this.setAnswer(e)
      }


      console.log(this.form)
    },
    setAnswer(el) {
      if (el === 1) { this.form.correctA = 1 }
      if (el === 2) { this.form.correctB = 1 }
      if (el === 3) { this.form.correctC = 1 }
      if (el === 4) { this.form.correctD = 1 }
      if (el === 5) { this.form.correctE = 1 }
      if (el === 6) { this.form.correctF = 1 }
    },
    typeChange(e) {
      console.log(e)
      // if (e == 3) {
      //   this.form.optionA = '正确'
      //   this.form.optionB = '错误'
      // } else {

      //   this.form.optionA = ''
      //   this.form.optionB = ''
      // }
    },
    handleClose() {
      this.reset()
    },
    addOption() {
      if (this.optionCount > 6) {
        this.$modal.msgSuccess("最多只能添加6个选项！");
      } else {
        this.optionCount++
      }
    },
    /** 查询题库列表 */
    getList() {
      this.loading = true;
      listBank(this.queryParams).then(response => {
        this.bankList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        subTitle: null,
        content: null,
        type: null,
        optionA: null,
        optionB: null,
        optionC: null,
        optionD: null,
        optionE: null,
        optionF: null,
        correctA: null,
        correctB: null,
        correctC: null,
        correctD: null,
        correctE: null,
        correctF: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        status: null,
        level: null,
        class: null,
        subjectId: null,
        subjectDeptId: null,
        subjectChapterDeptId: null
      };
      this.formTypeState = false
      this.correctAnswer = undefined
      this.correctAnswerMul = undefined
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.subjectId = this.subjectId;
      this.form.subjectDeptId = this.subjectDeptId;
      this.title = "添加题库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBank(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改题库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBank(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBank(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除题库编号为"' + ids + '"的数据项？').then(function () {
        return delBank(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bank/export', {
        ...this.queryParams
      }, `bank_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.w200 {
  width: 160px;
}
</style>
